package com.whl.org.controller;


import com.whl.basic.annotation.PreAuthorize;
import com.whl.basic.util.AjaxResult;
import com.whl.basic.util.PageList;
import com.whl.org.domain.Employee;
import com.whl.org.query.EmployeeQuery;
import com.whl.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：员工
 */
@RestController
@RequestMapping("/employee")
@Api(value = "员工接口Api", description = "员工接口的CRUD方法")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 接口：查询单个对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
//    @PreAuthorize(name = "查询单个对象", sn = "employee:getOne")
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.findOne(id);
    }

    /**
     * 接口：查询所有对象
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有对象")
//    @PreAuthorize(name = "查询所有对象", sn = "employee:getAll")
    public List<Employee> queryAll(){
        return employeeService.findAll();
    }

    /**
     * 接口：分页和高级查询
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页和高级查询")
//    @PreAuthorize(name = "员工列表", sn = "employee:list")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return employeeService.queryPage(query);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
//    @PreAuthorize(name = "删除员工", sn = "employee:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：添加和修改
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加和修改")
//    @PreAuthorize(name = "员工保存", sn = "employee:save")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){ // 添加操作
                employeeService.add(employee);
            }else { // 修改操作
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
//    @PreAuthorize(name = "员工批量删除", sn = "employee:patchDelete")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：接单时显示当前店铺所有员工
     * @param shopId
     * @return
     */
    @GetMapping("/list/{shopId}")
    @ApiOperation(value = "查询单个对象")
//    @PreAuthorize(name = "查询单个对象", sn = "employee:getOne")
    public List<Employee> list(@PathVariable("shopId") Long shopId){
        return employeeService.list(shopId);
    }
}
